import time
import traceback

from flask import jsonify, g
from app.libs.error_code import Success
from app.libs.redprint import Redprint
from app.libs.token_auth import auth
from app.models.order_info import order_info
from app.models.report_info import report_info
from app.models.report_source_info import report_source_info
from app.models.sentence import sentence
from app.models.sentence_detail import SentenceDetail
from app.models.sentences_word import SentenceWord
from app.validators.report import DeleteReportForm, ReportFullTextForm, SentenceInfoForm, FullTextForm
from app.view_models.Fulltext import PasteFullTextListView, DocumentFullTextListView
from app.models.base import db

api = Redprint('report')


@api.route('/fulltext', methods=['POST'])
@auth.login_required
def fulltext():
    form = FullTextForm(g).validate_for_api()

    if form.order_number.data[0] == '8':
        view = DocumentFullTextListView()
        fulltext = SentenceWord.query.filter_by(order_number=form.order_number.data).order_by(SentenceWord.id).paginate(
            form.page.data,
            500,
            error_out=False)
        view.fill(fulltext)
        return jsonify(view)
    if form.order_number.data[0] == '9':
        view = PasteFullTextListView()
        fulltext = sentence.query.filter_by(order_number=form.order_number.data).order_by(sentence.id).paginate(
            form.page.data,
            500,
            error_out=False)
        view.fill(fulltext)
        return jsonify(view)


@api.route('/sentence_info', methods=['POST'])
def sentence_info():
    form = SentenceInfoForm().validate_for_api()

    sentences = SentenceDetail.query.filter_by(order_number=form.order_number.data).filter_by(
        sentence_index=form.sentence_index.data).order_by(SentenceDetail.rate.desc()).limit(10).all()

    return jsonify(sentences)


@api.route('/report_info', methods=['POST'])
def get_report_info():
    form = ReportFullTextForm().validate_for_api()
    info = report_info.query.filter_by(order_number=form.order_number.data).first()
    return jsonify(info)


@api.route('/report_source_info', methods=['POST'])
def get_report_source_info():
    form = ReportFullTextForm().validate_for_api()
    local_infos = report_source_info.query.filter_by(order_number=form.order_number.data).filter_by(
        source_type='local').all()
    internet_infos = report_source_info.query.filter_by(order_number=form.order_number.data).filter_by(
        source_type='internet').all()
    res = {
        'local': local_infos,
        'internet': internet_infos
    }
    return jsonify(res)


@api.route('/delete',methods=['POST'])
@auth.login_required
def deleteReport():
    form = DeleteReportForm().validate_for_api()
    with db.auto_commit():
        now = int(time.time())
        report_info.query.filter_by(order_number=form.order_number.data).update({
            'status': now
        })
        order_info.query.filter_by(order_number=form.order_number.data).update({
            'status': now
        })
    return Success()
